"""
ui自动化的封装
Powered By cc
2020/04/94
"""
import time

from selenium.webdriver import ActionChains
from selenium.webdriver.chrome.options import Options
from selenium import webdriver


class WebKeywords():
    """
    ui自动化的关键字
    Powered By cc
    2020/04/05
    """
    def __init__(self):
        #驱动浏览器的driver
        self.__driver = webdriver.Chrome()



    def openBrowser(self,br='chrome',dr=None):
        """
        使用什么驱动打开浏览器，支持chrome和firfox，默认是chrome
        :param br: 浏览器类型，默认chorome
        :param dr: 驱动的地址，默认是None
        :return:
        """
        #驱动地址的设定
        if dr is None or dr =='':
            if br == 'chrome':
                dr = '../lib/webdriver/chromedriver.exe'
            if br == 'firfox':
                dr = '../lib/webdriver/geckodriver.exe'
        try:

            if br == 'chrome':

                option = Options()
                #使用chrome的缓存
                option.add_argument('--user-data-dir=D:\chromedata')
                #打开chrome浏览器
                self.__driver = webdriver.Chrome(options=option,executable_path=dr)
                #设置隐式等待时间10秒
                self.__driver.implicitly_wait(10)
            if br == 'firfox':
                self.__driver = webdriver.Firefox(executable_path=dr)
                self.__driver.implicitly_wait(10)
        except Exception as e :
            print(e)

    def getUrl(self,url):
        """
        打开指定的url
        :param url:
        :return:
        """
        self.__driver.get(url)

    def __findEle(self,xpath):
        """
        通过xpath定位元素
        :param xpath: 表达式
        :return: 返回定位到的元素
        """
        ele = self.__driver.find_element_by_xpath(xpath)
        return ele
    def __findEles(self,xpath):
        """
        通过xpath找多个元素
        :param xpath:
        :return: 多个元素的列表
        """
        ele = self.__driver.find_elements_by_xpath(xpath)
        return ele

    def click(self,xpath):
        """
        通过xpath定位点击操作
        :param xpath:
        :return:
        """
        try:
            ele = self.__findEle(xpath)
            ele.click()
        except Exception as e:
            print(e)

    def input(self,xpath,value):
        """
        通过xpath定位并输入输入值
        :param xpath:
        :param value: 输入的值
        :return:
        """
        try:
            ele = self.__findEle(xpath)
            ele.send_keys(value)
        except Exception as e:
            print(e)
    def getTitle(self):
        """
        返回当前window的名字
        :return:
        """
        return self.__driver.title

    def switchWindow(self,windowName):
        """
        通过window的名字切换window
        :param windowName:
        :return:
        """
        #获取当前的window，切换失败或者名字不存在，就切换回当前的window
        curWin = self.__driver.current_window_handle
        try:

            windows = self.__driver.window_handles
            for window in windows:
                self.__driver.switch_to.window(window)
                #匹配到了就直接break，切换成功
                if self.__driver.title == windowName:
                    break
            else:
                #所有的名称没有匹配到切换回默认的window
                self.__driver.switch_to.window(curWin)
        except Exception as e:
            #保错也切回原来的window
            print(e)
            self.__driver.switch_to.window(curWin)

    def switchFrame(self,xpath):
        """
        通过xapth切换框架
        :param xpath:
        :return:
        """
        try:

            ele = self.__findEle(xpath)
            #切换到指定的frame
            self.__driver.switch_to.frame(ele)
        except Exception as e:
            print(e)

    def switchDefFrame(self):
        """
        切回默认的框架
        :return:
        """
        try:
            #切回默认框架
            self.__driver.switch_to.default_content()
        except Exception as e:
            print(e)
    def moveToele(self,xpath):
        """
        鼠标悬停到某个元素
        :param xpath:
        :return:
        """
        try:

            ele = self.__findEle(xpath)
            #需要使用动作链类来执行一系列的操作
            act = ActionChains(self.__driver)
            #移动到某个元素上面
            act.move_to_element(ele)
            #执行
            act.perform()
        except Exception as e:
            print(e)


    def quit(self):
        """
        关闭浏览器
        :return:
        """
        self.__driver.quit()

    def mustSleep(self,t=1):
        """
        强制进程等待时间，默认1秒
        :param t:
        :return:
        """
        try:

            if t is None or t =='':
                t = 1
            time.sleep(int(t))
        except Exception as e:
            print(e)



if __name__ == '__main__':
    web = WebKeywords()
    web.openBrowser()
    web.quit()

